package com.example.excelUtil;

import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.example.excelUtil.vo.InquiryPriceExcelVO;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 1. @Description
 * 2. @Author wj
 * 3. @Date 2024/3/25 18:09
 * 4. @Version 1.0
 */
public class ExcelUtils {

    /**
     * @Description 导出模板
     * @Author wj
     * @Date 2024/3/25 18:09
     * @Version 1.0
     */
    public void getProcureFile(Map<String, Object> params, HttpServletResponse response) {
        List<InquiryPriceExcelVO> list2 = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            //基本信息列表
            InquiryPriceExcelVO inquiryPriceExcelVO = new InquiryPriceExcelVO();
            inquiryPriceExcelVO.setNum(i+1);
            inquiryPriceExcelVO.setInquiryNumber("11111");
            inquiryPriceExcelVO.setGoodsName("商品名称"+i);
            inquiryPriceExcelVO.setInquiryGoodsNumber("询源商品编号"+i);
            inquiryPriceExcelVO.setGoodsDescribe("询源商品编号"+i);
            inquiryPriceExcelVO.setGoodsVersion("询源商品编号"+i);
            inquiryPriceExcelVO.setQuantityUnit("询源商品编号"+i);
            inquiryPriceExcelVO.setBrandName("询源商品编号"+i);
            inquiryPriceExcelVO.setProcureNumber(i);
            inquiryPriceExcelVO.setBudgetPrice("i");
            inquiryPriceExcelVO.setBudgetSumPrice("11111");
            inquiryPriceExcelVO.setProductArea("11111");
            inquiryPriceExcelVO.setNotes("11111");
            list2.add(inquiryPriceExcelVO);
        }
        InputStream is = null;
        // 取出模板
        try {
            is = ResourceUtil.getStream("classpath:templates/inquiryPrice/shopInquiryPrice.xlsx");
            // 这里定义最终导出的文件名称
            String fileName = "供应商报价单";
            ExcelWriter excelWriter = EasyExcel.write(TemplateExcelUtils.getOutputStream(fileName, response)).withTemplate(is).excelType(ExcelTypeEnum.XLSX).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            // 这个 data 是官方约定就这么写， 传进去的 list 就是要遍历的表格数据，详见我定义的模板中图片位置右侧那部分
            excelWriter.fill(new FillWrapper("data", list2), writeSheet);
            excelWriter.finish();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
